package cn.demengsite.controller;

import cn.demengsite.model.Admin;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.service.AdminService;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import static cn.demengsite.base.APPCONST.ADMIN;

@Controller
public class AdminController {

    private   final Logger logger = Logger.getLogger(AdminController.class.getName());

    @Autowired
    private AdminService  adminService;


    @GetMapping("/admin/showAdmin")
    public String showAdmin(HttpServletRequest request){

        Admin admin = (Admin) request.getSession().getAttribute(ADMIN);
        if(admin==null || admin.getName()==null){
            return "admin/login";
        }else{
            //如果是已经登录，则开始查看信息
        }
        return "admin/index";
    }
    @GetMapping("/adminc/login")
    @ResponseBody
    public String login(HttpServletRequest request,
                        @Param("name") String name,
                        @Param("password") String password){

        if(password.equals("888888")){
            Admin admin = new Admin(name,password);
            request.getSession().setAttribute(ADMIN,admin);
        }

        return "/admin/showAdmin";
    }

    @GetMapping("/admin/getSaleDataWithBeginEnd/{beginDate}/{endDate}")
    @ResponseBody
    public String getSaleDataWithBeginEnd(@PathVariable String beginDate, @PathVariable String endDate){
        logger.info("come in getSaleDataWithBeginEnd");
        BaseInfo baseInfo = adminService.getAdminSaleWithBeginEnd(beginDate, endDate);

        return JSON.toJSONString(baseInfo.getObject());
    }
}
